'use strict';

var arduinoControllers = angular.module('arduinoControllers', []);

arduinoControllers.controller('MainCtrl', function($scope, httpCallService) {
  console.log($scope);
  $scope.isMobileMode = getMobileMode(window.innerWidth);
  $scope.titre = "Accueil";
  $scope.boolAff = false;
  $scope.displayList = false;
  $scope.today = function() {
    $scope.dt = new Date();
  };
  $scope.today();
  $scope.retour = "";
  /*
   $scope.$on(window.resize, function() {
   console.log('resize');
   $scope.isMobileMode = getMobileMode(window.innerWidth);
   }
   );*/


  window.onresize = function() {
    $scope.isMobileMode = getMobileMode(window.innerWidth);
    $scope.$apply();
  };

  $scope.appel = function(parametre) {
    httpCallService.setParametre(parametre);
    httpCallService.httpCall().then(function(msg) {
      $scope.retour = msg;
    });
  };
});

arduinoControllers.controller('MenuCtrl', function($scope, $location) {
  $scope.$parent.titre = "Menu";

  $scope.menu = function(p) {
    console.log('menu');
    $location.path('/' + p);
  };
});

arduinoControllers.controller('ConfigCtrl', function($scope, listeArduinos, UrlServiceRest) {
  $scope.$parent.titre = "Configuration";
  $scope.visible = false;
  $scope.isError = false;
  $scope.url = "";

  $scope.rafraichir = function() {
    $scope.isError = false;
    $scope.indexReponse = 0;
    $scope.displayList = false;
    UrlServiceRest.setUrlServiceRest($scope.url);
    listeArduinos.refreshArduinos().then(function(msg) {
      if (listeArduinos.getIsError() === false) {
        $scope.indexReponse = 1;
        $scope.arduinos = new Array();
        $scope.arduinos = listeArduinos.getArduinos();
        if ($scope.arduinos.length === 0) {
          $scope.indexReponse = 3;
          $scope.info = "Aucune arduino connectée";
        }
      } else {
        $scope.indexReponse = 2;
        $scope.info = "Erreur de connexion au serveur";
        $scope.isError = true;
      }
      console.log(listeArduinos.getIsError());
    });
    $scope.visible = !$scope.visible;
  };
});

arduinoControllers.controller('BlinkCtrl', function($scope, listeArduinos, httpCallService, parametreCall) {
  $scope.$parent.titre = "Clignoter la LED";
  $scope.liste = "Allez D'abord dans config pour actualiser la liste des Arduinos connectées.";
  $scope.arduinos = listeArduinos.getArduinos();
  $scope.retourFirstController = "";
  $scope.reponses = [];
  $scope.info = "Pas d'arduinos connectées";


  $scope.valider = function() {
    //$scope.retourFirstController = "";
    $scope.indexReponse = 0;
    parametreCall.init();
    // blink/{idCommande}/{idArduino}/{pin}/{duree}/{nombre}
    if (typeof ($scope.arduinos) !== "undefined") {
      $scope.displayList = false;

      for (var index in $scope.arduinos) {
        if ($scope.arduinos[index].checked === true) {
          var parametre = "blink/" + "1" + "/" + $scope.arduinos[index].ip + "/" + $scope.numPin +
                  "/" + $scope.tpsCligno + "/" + $scope.nbCligno;
          parametreCall.addParametre(parametre);
          console.log("PARAMETRE");
          console.log(parametreCall.getParametres());
        }
      }
      
      if (parametreCall.getParametres().length !== 0) {
        var promise = httpCallService.httpCall(parametreCall.getParametres());
        promise.then(function(successCallback) {
          $scope.indexReponse = 1;
          $scope.reponses = successCallback;
          console.log(successCallback);
        },function(errorCallback) {
          $scope.indexReponse = 2;
          $scope.reponses = errorCallback;
        });
      }
    }
  };
});

arduinoControllers.controller('PinWriteCtrl', function($scope, listeArduinos, httpCallService, parametreCall) {
  $scope.$parent.titre = "Ecrire sur une pin";
  $scope.retour = "";
  $scope.liste = "Allez D'abord dans config pour actualiser la liste des Arduinos connectées.";
  $scope.arduinos = listeArduinos.getArduinos();
  $scope.mode = "";
  $scope.boolBin = false;
  $scope.boolAna = false;
  $scope.retourFirstController = "";
  $scope.reponses = [];
  
  $scope.valider = function() {
    $scope.retourFirstController = "";
    $scope.indexReponse = 0;
    $scope.displayList = false;
    parametreCall.init();
    // blink/{idCommande}/{idArduino}/{pin}/{duree}/{nombre}
    if (typeof ($scope.arduinos) !== "undefined") {
      if ($scope.arduinos.length !== 0) {
        if (typeof ($scope.val) !== "undefined" && typeof ($scope.numPin) !== "undefined") {
          //construction parametres par arduinos
          for (var index in $scope.arduinos) {
            if ($scope.arduinos[index].checked === true) {
              var parametre = "pinWrite/" + "1" + "/" + $scope.arduinos[index].ip + "/" + $scope.numPin +
                      "/" + $scope.mode + "/" + $scope.val + "/";
              parametreCall.addParametre(parametre);
              console.log("PARAMETRE");
              console.log(parametreCall.getParametres());
            }
          }
          // for verif checked arduinos $scope.arduinos[0].
          if (parametreCall.getParametres().length !== 0) {
            var promise = httpCallService.httpCall(parametreCall.getParametres());
            promise.then(function(successCallback) {
              $scope.indexReponse = 1;
              $scope.reponses = successCallback;
            }, function(errorCallback) {
              $scope.indexReponse = 2;
              $scope.reponses = errorCallback;
            });
          }
          ;
        } else {
          $scope.indexReponse = 2;
          $scope.info = "Vous devez configurer le mode, la pin et sa valeur";
        }
      } else {
        $scope.indexReponse = 2;
        $scope.info = "Aucune arduino de connectéee";
      }
    }
  };

  $scope.modeFct = function(i) {
    if (i === 1) {
      $scope.mode = "b";
      $scope.boolBin = true;
      $scope.boolAna = false;
    } else if (i === 0) {
      $scope.mode = "a";
      $scope.boolBin = false;
      $scope.boolAna = true;
    }
  };
});

arduinoControllers.controller('PinReadCtrl', function($scope, listeArduinos, httpCallService, parametreCall) {
  $scope.$parent.titre = "Lire une valeur";
  $scope.liste = "Allez D'abord dans config pour actualiser la liste des Arduinos connectées.";
  $scope.arduinos = listeArduinos.getArduinos();
  $scope.retourFirstController = "";
  $scope.reponses = [];
  $scope.temperature = 0;
  $scope.temperatureBool = false;
  $scope.boolBin = false;
  $scope.boolAna = false;

  $scope.valider = function() {
    $scope.retourFirstController = "";
    $scope.indexReponse = 0;
    $scope.displayList = false;
    parametreCall.init();
    if (typeof ($scope.arduinos) !== "undefined") {
      $scope.displayList = false;
      for (var index in $scope.arduinos) {
        if ($scope.arduinos[index].checked === true) {
          var parametre = "pinRead/" + "1" + "/" + $scope.arduinos[index].id + $scope.arduinos[index].ip + "/" + 
                  $scope.numPin + "/" + $scope.mode;
          parametreCall.addParametre(parametre);
        }
      }
      if (parametreCall.getParametres().length !== 0) {
        var promise = httpCallService.httpCall(parametreCall.getParametres());
        promise.then(function(successCallback) {
          $scope.indexReponse = 1;
          $scope.reponses = successCallback;
        },function(errorCallback) {
          $scope.indexReponse = 2;
          $scope.retourFirstController = errorCallback;
        });
      }
    }
    if ($scope.temperatureBool) {
      var p = $scope.retourFirstController;
      $scope.temperature = $scope.calculerTemp(p);
      $scope.temperatureBool = false;
    }
  };

  $scope.modeFct = function(i) {
    if (i === 1) {
      $scope.mode = "b";
      $scope.boolBin = true;
      $scope.boolAna = false;
    } else if (i === 0) {
      $scope.mode = "a";
      $scope.boolBin = false;
      $scope.boolAna = true;
    } else if (i === 2) {
      $scope.mode = "a";
      $scope.boolBin = false;
      $scope.boolAna = true;
      $scope.temperatureBool = true;
    }
  };

  $scope.calculerTemp = function(p) {
    var voltage = p * 5000 / 255;
    return (voltage - 500) / 10;
  };
});

arduinoControllers.controller('CommandCtrl', function($scope, listeArduinos, parametreCall, httpCommandService) {
  $scope.$parent.titre = "Commande Json";
  $scope.retour = "retourPost";
  $scope.arduinos = listeArduinos.getArduinos();

  $scope.valider = function() {
    parametreCall.init();
    $scope.retourFirstController = "";
    $scope.displayList = false;
    if (typeof ($scope.arduinos) !== "undefined") {
      for (var index in $scope.arduinos) {
        if ($scope.arduinos[index].checked === true) {
          var parametre = "commands/" + $scope.arduinos[index].ip;
          parametreCall.addParametre(parametre);
        }
      }
      var promise = httpCommandService.httpCall(parametre, $scope.cmdJSON);
      promise.then(function(successCallback) {
        console.log("SUCCESS");
        $scope.reponses = successCallback;
      }, function(errorCallback) {
        console.log("ERROR");
        $scope.reponses = errorCallback;
      });
    }
  };
});

function getMobileMode(width) {
  if (width < 750) {
    return true;
  } else {
    return false;
  }
}
